package org.example.myleet.p788;

import org.example.myleet.Utils.Assert;
import org.example.myleet.Utils.JsonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    private static final Map<Integer, Integer> FLIPPING = new HashMap<>();

    static {
        FLIPPING.put(0, 0);
        FLIPPING.put(1, 1);
        FLIPPING.put(8, 8);
        FLIPPING.put(2, 5);
        FLIPPING.put(5, 2);
        FLIPPING.put(6, 9);
        FLIPPING.put(9, 6);
    }

    private static int[] RES = new int[10001];

    public static void main(String[] args) {
        Solution solution = new Solution();
        Assert.isTrue(4 == solution.rotatedDigits(10));
        Assert.isTrue(2320 == solution.rotatedDigits(9999));
    }

    public static void main2(String[] args) {
        List<Integer> res = new ArrayList<>();
        for (int i = 1; i <= 10000; ++i) {
            boolean canFlip = true;
            char[] originDigits = Integer.toString(i).toCharArray();
            char[] flippedDigits = new char[originDigits.length];
            for (int j = 0; j < originDigits.length; ++j) {
                int digit = originDigits[j] - '0';
                Integer flipped = FLIPPING.get(digit);
                if (null == flipped) {
                    canFlip = false;
                    break;
                }
                flippedDigits[j] = (char) (flipped + '0');
            }
            if (canFlip) {
                boolean identical = true;
                for (int j = 0; j < originDigits.length; ++j) {
                    if (originDigits[j] != flippedDigits[j]) {
                        identical = false;
                    }
                }
                if (identical) {
                    RES[i] = RES[i - 1];
                } else {
                    RES[i] = RES[i - 1] + 1;
                    res.add(i);
                }
            } else {
                RES[i] = RES[i - 1];
            }
        }
        System.out.println(JsonUtils.toJson(RES));
        System.out.println(JsonUtils.toJson(res));
    }
}
